---
title: "Graphs for Confucian Long Peace"
author: "Daniel Druckman, Michael Gigante, Joshua Stone, Ming Wan (Order TBD)"
output:
  pdf_document:
    latex_engine: lualatex
  html_document:
    df_print: paged
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
my.dir <- dirname(rstudioapi::getActiveDocumentContext()$path)
setwd(my.dir)
library(tidyverse)
library(knitr)
library(AER)
library(ggplot2)
library(dplyr)
library(ggpubr)
library(kableExtra)
library(xtable)
library(stargazer)
library(summarytools)
```

## Data cleaning 
```{r echo=FALSE, fig.align='center', message=FALSE, warning=FALSE, results='asis'}
# reading in the data
df <- read.csv("data/appendix_a_dataset.csv")

# changing the NAs to 0's
df[is.na(df)] <- 0

## making datasets for japan, china, korea, and vietnam
df_japan <- df %>% filter(country == "japan")%>% dplyr::select(-c(event_info, sources, gov_type, year, event_id))
df_china <- df %>% filter(country == "china")%>% dplyr::select(-c(event_info, sources, gov_type, year, event_id))
df_korea <- df %>% filter(country == "korea")%>% dplyr::select(-c(event_info, sources, gov_type, year, event_id))
df_vietnam <- df %>% filter(country == "vietnam") %>% dplyr::select(-c(event_info, sources, gov_type, year, event_id))

df_period_1 <- df %>% filter(year >=1598 & year <=1677 )%>% dplyr::select(-c(event_info, sources, gov_type, year, event_id))
df_period_2 <- df %>% filter(year >= 1678 & year <= 1769)%>% dplyr::select(-c(event_info, sources, gov_type, year, event_id))
df_period_3 <- df %>% filter(year >= 1770 & year <= 1860)%>% dplyr::select(-c(event_info, sources, gov_type, year, event_id))
df_period_4 <- df %>% filter(year >= 1861 & year <= 1894) %>% dplyr::select(-c(event_info, sources, gov_type, year, event_id))

## descriptive statistics for each country
stargazer(df[, -which(names(df) %in% c("event_id", "year"))], type= "latex", summary.stat = c("n", "min", "max", "mean", "sd"), header=FALSE, add.lines = list(c("Years" = "1598 - 1894")), title="Descriptive Statistics for all countries, pooled")

## descriptive statistics for Japan
stargazer(df_japan, type= "latex", summary.stat = c("n","min", "max", "mean", "sd"), header=FALSE, add.lines = list(c("Years" = "1598 - 1894")), title="Descriptive Statistics for Japanese events") 

## descriptive statistics for China
stargazer(df_china, type= "latex", summary.stat = c("n", "min", "max", "mean", "sd"), header=FALSE, add.lines = list(c("Years" = "1598 - 1894")), title="Descriptive Statistics for Chinese events")

## descriptive statistics for Korea
stargazer(df_korea, type= "latex", summary.stat = c("n","min", "max", "mean", "sd"), header=FALSE, add.lines = list(c("Years" = "1598 - 1894")), title="Descriptive Statistics for Korean events") 

## descriptive statistics for Vietnam
stargazer(df_vietnam, 
          type= "latex", 
          summary.stat = c("n","min", "max", "mean", "sd"), 
          header=FALSE, 
          title="Descriptive Statistics for Vietnam events")

#### YEARS
## descriptive statistics for period 1
stargazer(df_period_1, type= "latex", summary.stat = c("n","min", "max", "mean", "sd"), header=FALSE, add.lines = list(c("Years" = "1598 - 1677")), title="Descriptive Statistics for Period 1 (1598-1677)") 

## descriptive statistics for period 2
stargazer(df_period_2, type= "latex", summary.stat = c("n","min", "max", "mean", "sd"), header=FALSE, add.lines = list(c("Years" = "1678 - 1769")), title="Descriptive Statistics for Period 2 (1678-1769)")

## descriptive statistics for period 3
stargazer(df_period_3, type= "latex", summary.stat = c("n", "min", "max", "mean", "sd"), header=FALSE, add.lines = list(c("Years" = "1770 - 1861")), title="Descriptive Statistics for Period 3 (1770-1860)") 

## descriptive statistics for period 4
stargazer(df_period_4, type= "latex", summary.stat = c("n", "min", "max", "mean", "sd"), header=FALSE, nobs = TRUE, add.lines = list(c("Years" = "1861 - 1894")), title="Descriptive Statistics for Period 4 (1861-1894)") 

```